using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharpObjects.Interfaces;
using SharpObjects.Configuration;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.RuleEngine;
using SharpObjects.RuleEngine.RuleElements;

namespace SharpObjects.RuleEngine.Rules
{
    public class SetValueRule : IXmlInitializable, IRule
    {
        public string Output { get; set; }
        public ValueElement Value { get; set; }

        #region IInitializable Members

        public void Initialize(System.Xml.XmlElement initializationElement)
        {
            XmlConfigurationProvider init = new XmlConfigurationProvider(initializationElement);

            Output = init.GetString("@output");
            Value = init.GetObject<ValueElement>("value", (ValueElement)null);

            if(Value == null)
            {
                string stringValue = init.GetString("@value");
                Value = new ValueElement() {IsInitialized = true, Value = stringValue, ValueType = eFieldType.String};
            }

            this.IsInitialized = true;
        }

        public bool IsInitialized { get; set; }

        #endregion

        #region IRule Members

        public void Execute(ref IRuleEngineContext ruleEngineContext, IDataProvider dataProvider)
        {
            ruleEngineContext.SetValue<object>(Output, Value.Value);
        }

        #endregion
    }
}
